package com.atguigu.gmall.item.controller;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@RestController
@RequestMapping("/api/item")
public class ItemApiController {

    @Autowired
    private ProductFeignClient productFeignClient;
    @Autowired
    private ListFeignClient listFeignClient;
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @GetMapping("/getItem/{skuId}")
    public Map<String,Object> getItem(@PathVariable Long skuId){
        Map<String,Object> result = new HashMap<>();

        //1、skuInfo 库存信息
        CompletableFuture<SkuInfo> skuInfoThread = CompletableFuture.supplyAsync(() -> {
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            result.put("skuInfo", skuInfo);
            return skuInfo;
        }, threadPoolExecutor);

        //2、查询三级分类
        CompletableFuture<Void> categoryViewThread = skuInfoThread.thenAcceptAsync(skuInfo -> {
            BaseCategoryView baseCategoryView =
                    productFeignClient.getBaseCategory(skuInfo.getCategory3Id());
            result.put("categoryView", baseCategoryView);
        }, threadPoolExecutor);

        //3、平台属性值以及选中红框
        CompletableFuture<Void> spuSaleAttrListThread = skuInfoThread.thenAcceptAsync(skuInfo -> {
            List<SpuSaleAttr> saleAttrValueChecked =
                    productFeignClient.getSaleAttrValueChecked(skuInfo.getId(), skuInfo.getSpuId());
            result.put("spuSaleAttrList", saleAttrValueChecked);
        }, threadPoolExecutor);

        //4、销售属性值组合及对应库存ID
        CompletableFuture<Void> valuesSkuJsonThread = skuInfoThread.thenAcceptAsync(skuInfo -> {
            Map valuesSkuJson =
                    productFeignClient.getValuesSkuJson(skuInfo.getSpuId());
            result.put("valuesSkuJson", JSONObject.toJSONString(valuesSkuJson));
        }, threadPoolExecutor);

        //5、单独查价格price,不需要返回值
        CompletableFuture<Void> priceThread = CompletableFuture.runAsync(() -> {
            BigDecimal price = productFeignClient.getPrice(skuId);
            result.put("price", price);
        }, threadPoolExecutor);

        //6、增加热度分
        CompletableFuture.runAsync(() -> {
            listFeignClient.hotScore(skuId);
        }, threadPoolExecutor);

        //等待线程执行
        CompletableFuture.allOf(categoryViewThread,spuSaleAttrListThread,
                valuesSkuJsonThread,priceThread).join();

        return result;
    }

}
